package com.zcf.service.impl;

import com.zcf.pojo.Banner;
import com.zcf.common.json.Body;
import com.zcf.mapper.BannerMapper;
import com.zcf.service.BannerService;
import com.zcf.utils.Hutool;
import com.zcf.utils.UploadImgUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

	@Autowired
	BannerMapper bannerMapper;

	@Override
	public Body insertBanner(Banner banner, MultipartFile[] file) {
		if (banner.getPostion() < 1) {
			return Body.newInstance(0, "位置不能小于1");
		}
		EntityWrapper<Banner> ew = new EntityWrapper<>();
		ew.eq("postion", banner.getPostion());
		ew.eq("type", 1);
		if (bannerMapper.selectList(ew).size() > 0) {
			return Body.newInstance(-1, "位置" + banner.getPostion() + "已经在使用，请先停用！");
		}

		EntityWrapper<Banner> ews = new EntityWrapper<>();
		ews.eq("din_id", banner.getDinId());
		ews.eq("type", 1);
		if (bannerMapper.selectList(ews).size() > 0) {
			return Body.newInstance(-2, "该商家已经轮播中");
		}
		
		Banner banner2 = new Banner();
		banner2.setId(Hutool.getId());
		banner2.setDinId(banner.getDinId());
		banner2.setPostion(banner.getPostion());
		if (banner.getIntro() != null) {
			banner2.setIntro(banner.getIntro());
		}
		banner2.setTime(Hutool.parseDateToString());
		try {
			String files = UploadImgUtils.uploadFiles(file);
			if (files != null && !"".equals(files)) {
				banner2.setPhth(files);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		boolean sum = banner2.insert();
		if (sum) {
			return Body.BODY_200;
		}
		return Body.BODY_500;
	}

}
